package com.ccibe.g16system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccibe.g16system.mapper.CustomerMapper;
import com.ccibe.g16system.pojo.Customer;
import com.ccibe.g16system.pojo.form.LoginForm;
import com.ccibe.g16system.service.CustomerService;
import com.ccibe.g16system.utils.SecretUtil;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer>
        implements CustomerService {

    @Override
    public Customer login(LoginForm form) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Customer::getAccount, form.getUsername())
                .eq(Customer::getPassword, SecretUtil.md5Encode(form.getPassword()));
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public boolean register(LoginForm form) {
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Customer::getAccount, form.getUsername());
        Customer customer = baseMapper.selectOne(wrapper);
        if (customer != null) return false;
        customer = new Customer();
        customer.setAccount(form.getUsername());
        customer.setPassword(SecretUtil.md5Encode(form.getPassword()));
        return baseMapper.insert(customer) > 0;
    }
}
